package cn.xx.auth.controller;

import cn.xx.auth.entity.User;
import cn.xx.auth.mapper.UserMapper;
import cn.xx.auth.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.executor.BatchResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @GetMapping("list")
    public Result<List<User>> list() {
        List<User> users = userMapper.selectList(new QueryWrapper<User>());
        return Result.ok(users);
    }
    @GetMapping("/get")
    public Result<?> get(@RequestParam(value = "username", required = false) String username) {
        return Result.ok(userMapper.selectOne(new QueryWrapper<User>().eq("username", username)));
    }

    @PostMapping("/insert")
    public Result<?> insert(@RequestBody User user) {
        return Result.ok(userMapper.insert(user));
    }

    @PostMapping("batchInsert")
    public Result<?> batchInsert(@RequestBody List<User> user) {
        List<BatchResult> insert = userMapper.insert(user);
        return Result.ok();
    }

    @PostMapping("/delete")
    public Result<?> delete(@RequestParam(value = "id", required = false) Integer id) {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id", id);
        return Result.ok(userMapper.delete(queryWrapper));
    }

}
